package com.fishcoco.app.yunhang.fragments.base;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.fishcoco.app.yunhang.R;

import me.yokeyword.fragmentation.ISupportFragment;


/**
 * Created by YoKeyword on 16/2/3.
 */
public abstract class BaseMainFragment extends BaseFragment implements View.OnClickListener {
    //    public ProgressDialog waitingDialog;
    private OnFragmentOpenDrawerListener mOpenDraweListener;

    private MaterialDialog findingDialog;
    private MaterialDialog loadingDialog;
    private AppCompatTextView findingDialogMsg;
    private AppCompatTextView loadingDialogMsg;

    protected void initToolbarNav(Toolbar toolbar) {
        initToolbarNav(toolbar, false);
    }

    protected void initToolbarNav(Toolbar toolbar, boolean isHome) {
        toolbar.setNavigationIcon(R.mipmap.ic_menu_white_24dp);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOpenDraweListener != null) {
                    mOpenDraweListener.onOpenDrawer();
                }
            }
        });

    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentOpenDrawerListener) {
            mOpenDraweListener = (OnFragmentOpenDrawerListener) context;
        } else {
//            throw new RuntimeException(context.toString()
//                    + " must implement OnFragmentOpenDrawerListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mOpenDraweListener = null;
    }

    @Override
    public void start(ISupportFragment toFragment) {
        hideSoftInput();
        super.start(toFragment);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void pop() {
        hideSoftInput();
        super.pop();
    }

    protected TextView $T(int id) {
        return (TextView) getContentView().findViewById(id);
    }

    protected ImageView $I(int id) {
        return (ImageView) getContentView().findViewById(id);
    }

    protected void initClickListener(int... ids) {
        for (int id : ids) {
            $(id).setOnClickListener(this);
        }
    }

    @Override
    public void onClick(View v) {
        // 判断当前点击事件与前一次点击事件时间间隔是否小于阙值
        if (isFastDoubleClick()) {
            return;
        }
    }

    public interface OnFragmentOpenDrawerListener {
        void onOpenDrawer();
    }

    // 防止快速点击默认等待时长为900ms
    private long DELAY_TIME = 900;
    private static long lastClickTime;

    private boolean isFastDoubleClick() {
        long time = System.currentTimeMillis();
        long timeD = time - lastClickTime;
        if (0 < timeD && timeD < DELAY_TIME) {
            return true;
        }
        lastClickTime = time;
        return false;
    }

    public void toast(String msg) {
        ToastUtils.showShort(msg);
    }

    public void loadingDialog(String msg) {
        if (loadingDialog == null) {
            boolean wrapInScrollView = true;
            View contentView = View.inflate(getMContext(), R.layout.dialog_loading, null);
            loadingDialogMsg = (AppCompatTextView) contentView.findViewById(R.id.tv_dialog_msg);
            loadingDialog = new MaterialDialog.Builder(getMContext())
                    .title(R.string.app_name)
                    .customView(contentView, wrapInScrollView)
                    .positiveText(R.string.close)
                    .build();
        }
        if (StringUtils.isTrimEmpty(msg)) {
            loadingDialogMsg.setText(getString(R.string.findfindfind));
        } else {
            loadingDialogMsg.setText(msg);
        }
        if (!loadingDialog.isShowing()) {
            loadingDialog.show();
        }
    }

    public void findingDialog(String msg) {
        if (findingDialog == null) {
            boolean wrapInScrollView = true;
            View contentView = View.inflate(getMContext(), R.layout.dialog_finding, null);
            findingDialogMsg = (AppCompatTextView) contentView.findViewById(R.id.tv_dialog_msg);
            findingDialog = new MaterialDialog.Builder(getMContext())
                    .customView(contentView, wrapInScrollView)
                    .positiveText(R.string.close)
                    .build();
        }
        if (StringUtils.isTrimEmpty(msg)) {
            findingDialogMsg.setText(getString(R.string.findfindfind));
        } else {
            findingDialogMsg.setText(msg);
        }
        if (!findingDialog.isShowing()) {
            findingDialog.show();
        }
    }

    public void dialogDismiss() {
        if (loadingDialog != null) {
            if (loadingDialog.isShowing()) {
                loadingDialog.dismiss();
            }
        }
        if (findingDialog != null) {
            if (findingDialog.isShowing()) {
                findingDialog.dismiss();
            }
        }
    }

}
